﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Empolyee_EmpolyeeDetail : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ID"] != null)
            {
                BindEmpolyeeData();
            }
        }
    }

    private void BindEmpolyeeData()
    {
        int empolyeeID = Convert.ToInt32(Request.QueryString["ID"]);

        var empolyee = new EmpolyeeDAL().GetEmpolyeeInfoByEmpolyeeID(empolyeeID);

        if (empolyee != null)
        {
            EmpolyeeName.Text = empolyee.Name;
            LoginName.Text = empolyee.LoginName;
            Password.Text = empolyee.Password;
            if(empolyee.RoleID != -1)
            {
DropDownList1.SelectedValue = empolyee.RoleID.ToString();
            }
            else
            {
                DropDownList1.SelectedValue = "4";
            }
            
            Barcode.Text = empolyee.Barcode;
            Enabled.Checked = empolyee.Status == 0 ? true : false;
            labAll.Text = empolyee.Other.ToString();
        }
    }

    protected void BindBranch(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
        {
            int empolyeeID = Convert.ToInt32(Request.QueryString["ID"]);

            var branchid = new EmpolyeeDAL().GetBranchesByEmpolyeeID(empolyeeID);

            if (branchid != null && branchid.Count > 0)
            {
                foreach (ListItem item in CheckBoxList1.Items)
                {
                    if (branchid.Contains(Convert.ToInt32(item.Value)))
                    {
                        item.Selected = true;
                        continue;
                    }

                    item.Selected = false;
                }
            }
        }
    }

    protected void SaveData_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
        {
            var empolyeeID = Convert.ToInt32(Request.QueryString["ID"]);

            new EmpolyeeDAL().UpdateEmpolyeeInfoByEmpolyeeID(EmpolyeeName.Text.Trim(), LoginName.Text.Trim(),
                                                      Password.Text.Trim(), Enabled.Checked ? 0 : 1,
                                                      Page.User.Identity.Name, Barcode.Text.Trim(), empolyeeID);

            new EmpolyeeBLL().UpdateEmpolyeeRole(empolyeeID, Convert.ToInt32(DropDownList1.SelectedValue));

            EmpolyeeDAL.RemoveBranchRelation(empolyeeID);

            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    EmpolyeeDAL.AddBranchRelation(empolyeeID, Convert.ToInt32(item.Value));
                }
            }

            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>saveOK();</script>");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangeEmpolyeeWage.aspx?id=" + Request.QueryString["ID"]);
    }
}